package com.hbean.service.impl;

import com.hbean.entity.SysAcc;
import com.hbean.mapper.SysAccMapper;
import com.hbean.service.SysAccService;
import com.hbean.system.config.SysConfig;
import com.hbean.utils.Condition;
import com.hbean.utils.HUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018/10/26 11:55
 * @ModificationHistory: （who whatTime doWhat）
 */

@Service
public class SysAccServiceImpl implements SysAccService {

    @Autowired
    private SysAccMapper sysAccMapper;

    @Override
    public String addAccessory(MultipartHttpServletRequest multipartRequest) {

        List<SysAcc> list = new ArrayList();
        StringBuilder fjIds = new StringBuilder();

        //所有文件的文件名
        Iterator<String> iterator = multipartRequest.getFileNames();
        while(iterator.hasNext()){
            //取得上传文件
            MultipartFile file = multipartRequest.getFile(iterator.next());
            if(file != null){
                String fileName = file.getOriginalFilename();//文件名称
                if(fileName.trim() != ""){
                    SysAcc sysAcc = new SysAcc();
                    String s = HUtil.dateToString(new Date(), "yyyyMMddHHmmss");

                    String newFileName = HUtil.uuidOf36() +"."+ fileName.substring(fileName.lastIndexOf(".") + 1);// 新的文件名称
                    try {
                        //动态路径
                        StringBuilder filePath = new StringBuilder();

                        filePath.append(SysConfig.filePath).append(s.substring(0,4)).append("/").append(s.substring(4,6)).append("/").append(s.substring(6,8));

                        //创建目录
                        File fileDir=new File(filePath.toString());
                        if (!fileDir.exists()) fileDir.mkdirs();

                        //存文件
                        filePath.append("/").append(newFileName);
                        file.transferTo(new File(filePath.toString()));

                        sysAcc.setSa00(HUtil.uuidOf18());
                        sysAcc.setSa10(s);
                        sysAcc.setSa02(filePath.toString());
                        sysAcc.setSa03(fileName);
                        sysAcc.setSa04(file.getSize());
                        sysAcc.setSa05(fileName.substring(fileName.lastIndexOf(".") + 1));
                        sysAcc.setSa06(newFileName);
                        list.add(sysAcc);

                        fjIds.append(sysAcc.getSa00()).append(",");

                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
        this.sysAccMapper.insertDatas(list);
        return fjIds.toString().substring(0,fjIds.toString().length()-1);
    }

    @Override
    public boolean insertData(SysAcc sysAcc) {
        return false;
    }

    @Override
    public boolean deleteData(String str) {
        return false;
    }

    @Override
    public boolean updateData(SysAcc sysAcc) {
        return false;
    }

    @Override
    public List<SysAcc> getDatas(Condition condition) {
        return this.sysAccMapper.selectDatas(condition);
    }

    @Override
    public SysAcc getData(Condition condition, int flag) {
        return null;
    }

    @Override
    public SysAcc getData(SysAcc sysAcc, int flag) {
        return null;
    }
}
